<template>
  <div>
    <div class="search-con">
      <input
        type="text"
        v-model="keywords"
        placeholder="请输入商品进行搜索"
        @keydown="onSearch($event)"
      />
    </div>
    <!-- 商品列表区域 -->
    <div class="goods-list">
      <router-link
        tag="div"
        :to="'/idssp/shops/goodsinfo/' + item.id"
        class="goods-item"
        v-for="item in reverseList"
        :key="item.id"
      >
        <!-- <img :src="item.img_url" /> -->
        <img :src="circleUrl + item.img_url" />
        <h1 class="title">{{ item.title }}</h1>
        <div class="info">
          <p class="price">
            <span class="new">￥{{ item.sell_price }}</span>
            <span class="old">￥{{ item.market_price }}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{ item.stock_quantity }}件</span>
          </p>
        </div>
      </router-link>

      <mt-button
        type="danger"
        size="large"
        style="margin-top: 10px"
        @click="getMore"
        v-if="finished"
        >加载更多</mt-button
      >
      <div class="no-data" v-else>没有更多数据了</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      circleUrl: "http://localhost:9528/admin/news_info/downloadPic?img_url=",
      keywords: "",
      page: 1, //默认展示第一页数据
      goodslist: [], //商品列表
      isloaded: false, //节流阀，默认为false，表示没有加载完毕
      listNum: 10, //展示的商品数
      finished: true, //显示加载更多
    };
  },
  created() {
    this.getGoodsListByPage();
  },
  computed: {
    reverseList() {
      return this.goodslist.reverse();
    },
  },
  methods: {
    onSearch(e) {
      if (e.keyCode == 13) {
        this.$http
          .get("/searchGoods", {
            params: {
              title: this.keywords,
            },
          })
          .then((res) => {
            if (res.data.errcode === 200) {
              const data = res.data.message;
              this.goodslist = data;
            } else {
              this.goodslist = [];
            }
          });
      }
    },
    getGoodsListByPage() {
      this.$http.get("/getGoods").then((res) => {
        if (res.data.errcode === 200) {
          let data = res.data.message;
          if (data.length > this.listNum) {
            this.goodslist = data.slice(0, this.listNum);
            this.finished = true;
          } else {
            this.goodslist = data;
            this.finished = false;
          }
          if (this.listNum > this.goodslist.length) {
            this.finished = false;
          }
        } else {
          this.goodslist = [];
        }
      });
    },
    getMore() {
      this.listNum += 10;
      this.getGoodsListByPage();
    },
  },
};
</script>

<style lang="scss" scoped>
.search-con {
  position: fixed;
  top: 40px;
  left: 0;
  right: 0;
  padding: 10px 10px 0 10px;
  background-color: #fff;
  z-index: 999;
  input {
    text-indent: 20px;
    height: 35px;
    background: url("../../assets/img/search.png") no-repeat;
    background-size: 7% 70%;
    background-position-x: left;
    background-position-y: center;
  }
}
.goods-list {
  margin-top: 50px;
  // 设置父盒子为弹性盒模型
  display: flex; //默认不会换行
  flex-wrap: wrap; //能换行
  justify-content: space-between; //贴边
  padding: 7px;
  padding-top: 0;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    margin-top: 7px;
    box-shadow: 0 0 7px #ccc;
    padding: 2px; //使得文字不贴边
    flex-direction: column; //使其能够按纵轴来贴边，让图片不要上移
    display: flex;
    justify-content: space-between;
    img {
      min-height: 170px;
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      overflow: hidden; //解决让其贴底边
      p {
        margin: 3px;
      }
      .price {
        .new {
          color: red;
          font-size: 16px;
          font-weight: bold;
          margin-right: 10px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 13px;
      }
    }
  }
  .no-data {
    width: 100%;
    height: 30px;
    text-align: center;
    color: grey;
    font-size: 14px;
    margin-top: 20px;
  }
}
</style>